package com.cn.service.server.Equipment.Impl;

import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.Equipment.BusinessDimensionConfiguration;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.Equipment.BusinessDimensionConfigurationDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.Equipment.BusinessDimensionConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BusinessDimensionConfigurationServiceImpl implements BusinessDimensionConfigurationService {
    private final BusinessDimensionConfigurationDao businessDimensionConfigurationDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public BusinessDimensionConfigurationServiceImpl(Sys_Sequence_Info_TDAO sysSequenceInfoTdao, BusinessDimensionConfigurationDao businessDimensionConfigurationDao) {

        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
        this.businessDimensionConfigurationDao = businessDimensionConfigurationDao;
    }

    @Override
    public List<BusinessDimensionConfiguration> selectBusinessDimensionConfiguration(int limit, int page, String dimensionCode, String dimensionName) {
        return businessDimensionConfigurationDao.selectBusinessDimensionConfiguration(limit, page, dimensionCode, dimensionName);
    }

    @Override
    public Result addBusinessDimensionConfiguration(int dimensionType, String dimensionName, int status, String description, List<BusinessDimensionConfiguration> businessDimensionConfigurationList) {
        List<BusinessDimensionConfiguration> businessDimensionConfigurations = businessDimensionConfigurationDao.selectAll();
        for(BusinessDimensionConfiguration busD: businessDimensionConfigurations){
            if (busD.getDimensionName().equals(dimensionName)){
                return Result.failure("500","维度名称重复");
            }

        }
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("BusinessDimensionConfigurationMainTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return com.cn.config.config.Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "BusinessDimensionConfigurationMainTable");
        int i = businessDimensionConfigurationDao.addBusinessDimensionConfiguration(ID, dimensionType, code, dimensionName, status, description);
        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        for (BusinessDimensionConfiguration bus : businessDimensionConfigurationList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("BusinessDimensionConfigurationSubTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "BusinessDimensionConfigurationSubTable");
            int i1 = businessDimensionConfigurationDao.addBusinessDimensionConfigurationChild(ID1, ID, bus.getDimensionItemID(), bus.getDimensionItemName());
            if (i1 == 0) {

                return Result.failure("500", "添加失败");
            }

        }
        return Result.success("200", "添加成功");
    }

    @Override
    public Result deleteBusinessDimensionConfiguration(List<BusinessDimensionConfiguration> idList) {

        for (BusinessDimensionConfiguration bus : idList) {
            int i = businessDimensionConfigurationDao.deleteBusinessDimensionConfiguration(bus.getID());
            int i1 = businessDimensionConfigurationDao.deleteBusinessDimensionConfigurationChild(bus.getID());
            if (i == 0 || i1 == 0) {
                return Result.failure("500", "删除失败");
            }
        }
        return Result.success("200", "删除成功");
    }

    @Override
    public List<BusinessDimensionConfiguration> selectBusinessDimensionConfigurationChild(int ID) {
        return businessDimensionConfigurationDao.selectBusinessDimensionConfigurationChild(ID);
    }

    @Override
    public Result updateBusinessDimensionConfiguration(int ID, int dimensionType, String dimensionName, int status, String description,List<BusinessDimensionConfiguration> businessDimensionConfigurationList) {
        List<BusinessDimensionConfiguration> businessDimensionConfigurations = businessDimensionConfigurationDao.selectAll();
        for(BusinessDimensionConfiguration busD: businessDimensionConfigurations){
            if (busD.getDimensionName().equals(dimensionName)&&busD.getID()!=ID){
                return Result.failure("500","维度名称重复");
            }

        }
        int i = businessDimensionConfigurationDao.updateBusinessDimensionConfiguration(ID, dimensionType, dimensionName, status, description);
        int i1 = businessDimensionConfigurationDao.deleteBusinessDimensionConfigurationChild(ID);
        if(i==0||i1==0){
            return Result.failure("500", "修改失败");
        }
        for (BusinessDimensionConfiguration bus : businessDimensionConfigurationList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("BusinessDimensionConfigurationSubTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "BusinessDimensionConfigurationSubTable");
            int i2 = businessDimensionConfigurationDao.addBusinessDimensionConfigurationChild(ID1, ID, bus.getDimensionItemID(), bus.getDimensionItemName());
            if (i2 == 0) {

                return Result.failure("500", "修改失败");
            }

        }
        return Result.success("200","修改成功");
    }
}
